package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.service.ApplyPositionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.entity.ApplyPosition;
import com.wanmait.wanmarecruit.mapper.ApplyPositionMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class ApplyPositionServiceImpl extends ServiceImpl<ApplyPositionMapper, ApplyPosition> implements ApplyPositionService {

    @Resource
    private ApplyPositionMapper applyPositionMapper;
    //通过userId查询所有的申请工作表（张佳炜）
    @Override
    public PageInfo<ApplyPosition> selectAllApplicationJobsByUserId(int pageNum,int pageSize, Integer userId ,String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<ApplyPosition> applyPositions = applyPositionMapper.selectAllApplicationsByUserId(userId ,keyword);
        return new PageInfo<>(applyPositions,5);
    }

    @Override
    public void deleteOneByApplyPositionId(Integer applyPositionId) {
        applyPositionMapper.deleteById(applyPositionId);
    }

    @Override
    public void deleteSelectByApplyPositionIds(String applyPositionIds) {
        applyPositionMapper.deleteSelectByApplyPositionIds(applyPositionIds);
    }

    @Override
    public PageInfo<ApplyPosition> selectAllApplyPostitonByCompanyId(int pageNum, int pageSize, Integer companyId, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<ApplyPosition> applyPositions = applyPositionMapper.selectAllApplyPositionByCompanyId(companyId, keyword);
        return new PageInfo<>(applyPositions,5);
    }

    @Override
    public Integer selectCountByUser(Integer userId) {
        LambdaQueryWrapper<ApplyPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApplyPosition::getUserId,userId);
        return (int)this.count(queryWrapper);
    }

    @Override
    public void updateApplyStausByApplyPositionIds(String applyPositionIds, Integer applyStaus) {
        applyPositionMapper.updateApplyStausByApplyPositionId(applyPositionIds,applyStaus);
    }

    @Override
    public void updateApplyPositionEnableByApplyPositionIds(String applyPositionIds) {
        applyPositionMapper.updateApplyPositionEnableByApplyPositionId(applyPositionIds);
    }

    @Override
    public Boolean checkApplyPositionById(Integer positionId, Integer userId) {
        LambdaQueryWrapper<ApplyPosition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApplyPosition::getUserId,userId);
        lambdaQueryWrapper.eq(ApplyPosition::getPositionId,positionId);
        lambdaQueryWrapper.eq(ApplyPosition::getEnable,true);
        return this.getOne(lambdaQueryWrapper)!=null;
    }

    @Override
    public void insertApplyPositionById(Integer positionId, Integer userId) {
        ApplyPosition applyPosition = new ApplyPosition();
        applyPosition.setPositionId(positionId);
        applyPosition.setUserId(userId);
        applyPositionMapper.insert(applyPosition);
    }

    @Override
    public Long getApplyPositionCount() {
        LambdaQueryWrapper<ApplyPosition> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ApplyPosition::getApplyStatus,2);
        return this.count(queryWrapper);
    }
}
